package com.credit.mola.app.analysis

import com.credit.mola.app.BuildConfig
import com.credit.mola.app.app.App
import com.credit.mola.app.base.BaseApp
import com.credit.mola.app.utils.analysis.AnalysisBuilder
import com.credit.mola.app.utils.analysis.AnalyticsUtil
import com.credit.mola.app.utils.config.Configs
import com.credit.mola.app.utils.http.AppUrls
import com.credit.mola.app.utils.http.AppUrls.addCommonParams
import com.credit.mola.app.utils.http.HttpHelper
import com.credit.mola.app.utils.http.IHttpCallback
import com.credit.mola.app.utils.sysdata.SysDataHelper
import org.json.JSONObject


/**
 * TODO: Analysis Lib 初始化时机
 */
object TestAnalysisInit {

    /**
     * TODO: APP启动时，初始化一下 analysis lib
     */
    fun initAnalysisLibAtAppLaunch() {
        AnalysisBuilder.product = Configs.AppName
        AnalysisBuilder.appVersion = BuildConfig.VERSION_CODE.toString()
        AnalysisBuilder.appPckName = BuildConfig.APPLICATION_ID

        AnalysisBuilder.aesKey = Configs.getSPString(Configs.SPKEY.CREDIT_MOLA_KEY)
        AnalysisBuilder.aesIv = Configs.getSPString(Configs.SPKEY.CREDIT_MOLA_IV)

        AnalysisBuilder.host = AppUrls.APP_URL
        AnalysisBuilder.zone = ""
        AnalysisBuilder.pathCommon = AppUrls.URL.COMMON

        AnalysisBuilder.pathGoogleInstall = AppUrls.ANALYSIS_PATH.ANALYSIS_GOOGLE_INSTALL_PATH// "/l4dgkLgM4OiraDvhSmmB6Uw"// /v1/channelInstallTemp
        AnalysisBuilder.pathAdjustInstall = AppUrls.ANALYSIS_PATH.ANALYSIS_ADJUST_INSTALL_PATH// "/zCazEswpalHGhY1RrROqA"// /v1/adjustInstallTemp
        AnalysisBuilder.pathFacebookInstall = AppUrls.ANALYSIS_PATH.ANALYSIS_FACEBOOK_INSTALL_PATH// "/P255np9RducZDhKlWCsnA"// /v1/saveFbAppData
        AnalysisBuilder.pathFirebaseInstall = AppUrls.ANALYSIS_PATH.ANALYSIS_FIREBASE_INSTALL_PATH// "/ChfSGvVETkNdSTtCMJ631Q"// /v1/fireBase/save

        AnalyticsUtil.init(BaseApp.application)
    }

    /**
     * TODO: 成功获取地区转发路径时，初始化一下 analysis lib
     */
    fun initAnalysisLibAtZoneSuccess() {
        AnalysisBuilder.host = AppUrls.APP_URL
        AnalysisBuilder.zone = Configs.getSPString(Configs.SPKEY.SIKADWA_TIMEZONE)
        AnalyticsUtil.init(BaseApp.application)
    }

    /**
     * TODO: 成功登录时，初始化一下 analysis lib
     */
    fun initAnalysisLibAtLoginSuccess() {
        AnalysisBuilder.token = Configs.getSPString(Configs.SPKEY.SIKADWA_TOKEN)
        AnalysisBuilder.userId = Configs.getSPString(Configs.SPKEY.SIKADWA_USERID)
        AnalyticsUtil.init(BaseApp.application)
    }
}

/**
 * TODO: Data Lib 调用时机
 */
object CreditMolaSysDataInit {

    /**
     * TODO: 在首页每次点击申请时（在已登录的前提下），调用一下 data lib 的上报
     * 获取设备数据上传状态
     */
    fun loadDeviceInfoTypeAction(){
        val params = JSONObject().addCommonParams().apply {
            put("path", AppUrls.PATH.DEVICE_INFO_TYPES)
        }
        val httpUrl = AppUrls.getHttpUrl(AppUrls.URL.COMMON)
        HttpHelper.post(httpUrl,params,object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                when(data.optInt("code")) {
                    0 -> {//成功
                        val obj = data.optJSONObject("obj")
                        obj?.let {
                            val deviceInfoTypes = obj.optString("deviceInfoTypes")
                            SysDataHelper.sysDataUpdateAction(App.instance(),deviceInfoTypes)
                        }
                    }
                }
            }
            override fun onRequestError(error: JSONObject) {
            }
            override fun onRequestFinish() {
            }
        })
    }
}